home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / STREAM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  12.8 KB  |  477 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* stream.c */
  21. /* Stream package for Ghostscript interpreter */
  22. #include <stdio.h>
  23. #include "memory_.h"
  24. #include "std.h"
  25. #include "stream.h"
  26. #include "scanchar.h"
  27.  
  28. /* Forward declarations */
  29.     /* Generic */
  30. /* Export these for filters */
  31. void
  32.   s_std_init(P5(stream *, byte *, uint, const stream_procs *, int /*mode*/));
  33. int
  34.   s_std_null(P1(stream *)),
  35.   s_std_noavailable(P2(stream *, long *)),
  36.   s_std_noseek(P2(stream *, long)),
  37.   s_std_close(P1(stream *));
  38.     /* Strings */
  39. private int
  40.   s_string_read_buf(P1(stream *)),
  41.   s_string_write_buf(P1(stream *)),
  42.   s_string_available(P2(stream *, long *)),
  43.   s_string_seek(P2(stream *, long));
  44. private void
  45.   s_string_init(P4(stream *, byte *, uint, const stream_procs *));
  46.     /* Files */
  47. private int
  48.   s_file_read_buf(P1(stream *)),
  49.   s_file_available(P2(stream *, long *)),
  50.   s_file_read_seek(P2(stream *, long)),
  51.   s_file_read_close(P1(stream *));
  52. private int
  53.   s_file_write_buf(P1(stream *)),
  54.   s_file_write_seek(P2(stream *, long)),
  55.   s_file_write_flush(P1(stream *)),
  56.   s_file_write_close(P1(stream *));
  57.  
  58. /* ------ Generic procedures ------ */
  59.  
  60. /* Standard stream initialization */
  61. void
  62. s_std_init(register stream *s, byte *ptr, uint len, const stream_procs *pp,
  63.   int modes)
  64. {    s->cbuf = ptr;
  65.     s->cptr = ptr - 1;
  66.     s->endptr = (!(modes & s_mode_read) ? s->cptr + len : s->cptr);
  67.     s->modes = modes;
  68.     s->end_status = 0;
  69.     s->position = 0;
  70.     s->bsize = s->cbsize = len;
  71.     s->strm = 0;            /* not a filter */
  72.     s->procs = *pp;
  73. }
  74.  
  75. /* Implement a stream procedure as a no-op. */
  76. int
  77. s_std_null(stream *s)
  78. {    return 0;
  79. }
  80.  
  81. /* Flush data to end-of-file when reading. */
  82. int
  83. s_std_read_flush(stream *s)
  84. {    while ( 1 )
  85.     {    s->cptr = s->endptr;
  86.         if ( s->end_status ) break;
  87.         (*s->procs.read_buf)(s);
  88.     }
  89.     return (s->end_status == EOFC ? 0 : s->end_status);
  90. }
  91.  
  92. /* Flush buffered data when writing. */
  93. int
  94. s_std_write_flush(stream *s)
  95. {    return (*s->procs.write_buf)(s);
  96. }
  97.  
  98. /* Indicate an error when asked for available input bytes. */
  99. int
  100. s_std_noavailable(stream *s, long *pl)
  101. {    return ERRC;
  102. }
  103.  
  104. /* Indicate an error when asked to seek. */
  105. int
  106. s_std_noseek(stream *s, long pos)
  107. {    return ERRC;
  108. }
  109.  
  110. /* Standard stream finalization.  Disable the stream. */
  111. int
  112. s_std_close(stream *s)
  113. {    s_disable(s);
  114.     return 0;
  115. }
  116. void
  117. s_disable(register stream *s)
  118. {    s->bsize = 0;
  119.     s->modes = 0;
  120.     /****** SHOULD DO MORE THAN THIS ******/
  121. }
  122.  
  123. /* ------ Implementation-independent procedures ------ */
  124.  
  125. /* Implement sgetc when the buffer may be empty. */
  126. /* If the buffer really is empty, refill it and then read a byte. */
  127. int
  128. spgetc(register stream *s)
  129. {    int code;
  130.     if ( !sendbufp(s) )
  131.         return *++(s->cptr);
  132.     if ( s->end_status )
  133.         return s->end_status;
  134.     code = (*s->procs.read_buf)(s);
  135.     if ( code < 0 )
  136.         return code;
  137.     if ( !sendbufp(s) )
  138.         return *++(s->cptr);
  139.     return (s->end_status ? s->end_status : EOFC);
  140. }
  141.  
  142. /* Implementing sputc when the buffer is full, */
  143. /* by flushing the buffer and then writing the byte. */
  144. int
  145. spputc(register stream *s, byte b)
  146. {    int code;
  147.     if ( s->end_status ) return s->end_status;
  148.     code = (*s->procs.write_buf)(s);
  149.     if ( code < 0 ) return code;
  150.     return sputc(s, b);
  151. }
  152.  
  153. /* Push back a character onto a (read) stream. */
  154. /* The character must be the same as the last one read. */
  155. /* Return 0 on success, ERRC on failure. */
  156. int
  157. sungetc(register stream *s, byte c)
  158. {    if ( !s_is_reading(s) || s->cptr < s->cbuf || *(s->cptr) != c )
  159.         return ERRC;
  160.     s->cptr--;
  161.     return 0;
  162. }
  163.  
  164. /* Read a string from a stream. */
  165. /* Return the number of bytes read. */
  166. uint
  167. sgets(register stream *s, byte *str, uint rlen)
  168. {    uint len = rlen;
  169.     while ( len > 0 )
  170.        {    uint count = sbufavailable(s);
  171.         if ( count == 0 )
  172.            {    int code;
  173.             if ( s->end_status )
  174.                 return rlen - len;
  175.             code = (*s->procs.read_buf)(s);
  176.             if ( code < 0 || sendbufp(s) )
  177.                 return rlen - len;
  178.             continue;
  179.            }
  180.         if ( count > len ) count = len;
  181.         memcpy(str, s->cptr + 1, count);
  182.         s->cptr += count;
  183.         str += count;
  184.         len -= count;
  185.        }
  186.     return rlen;
  187. }
  188.  
  189. /* Write a string on a stream. */
  190. /* Return the number of bytes written. */
  191. uint
  192. sputs(register stream *s, const byte *str, uint wlen)
  193. {    uint len = wlen;
  194.     if ( wlen > s->bsize && s->procs.write_buf == s_file_write_buf )
  195.        {    /* Write directly on the file. */
  196.         uint write_count;
  197.         (*s->procs.write_buf)(s);
  198.         write_count = fwrite(str, 1, wlen, s->file);
  199.         if ( s_can_seek(s) )
  200.             s->position = ftell(s->file);
  201.         return write_count;
  202.        }
  203.     while ( len > 0 )
  204.        {    uint count = sbufavailable(s);
  205.         if ( count > 0 )
  206.            {    if ( count > len ) count = len;
  207.             memcpy(s->cptr + 1, str, count);
  208.             s->cptr += count;
  209.             str += count;
  210.             len -= count;
  211.            }
  212.         else
  213.            {    byte ch = *str++;
  214.             sputc(s, ch);
  215.             if ( s->end_status ) return wlen - len;
  216.             len--;
  217.            }
  218.        }
  219.     return wlen;
  220. }
  221.  
  222. /* Read a hex string from a stream. */
  223. /* Answer EOFC if we reached end-of-file before filling the string, */
  224. /* 0 if we filled the string first, or ERRC on error. */
  225. /* s->odd should be -1 initially: */
  226. /* if an odd number of hex digits was read, s->odd is set to */
  227. /* the odd digit value, otherwise s->odd is set to -1. */
  228. /* If ignore_garbage is true, characters other than hex digits are ignored; */
  229. /* if ignore_garbage is false, characters other than hex digits or */
  230. /* whitespace return an error. */
  231. int
  232. sreadhex(stream *s, byte *str, uint rlen, uint *nread,
  233.   int *odd_digit, int ignore_garbage)
  234. {    byte *ptr = str;
  235.     byte *limit = ptr + rlen;
  236.     byte val1 = (byte)*odd_digit;
  237.     byte val2;
  238.     byte save_last;
  239.     register byte _ds *decoder = scan_char_decoder;
  240.     s_declare_inline(s, sptr, endp);
  241.     int ch;
  242.     int code;
  243.     if ( rlen == 0 )
  244.        {    *nread = 0;
  245.         return 0;
  246.        }
  247.     s_begin_inline(s, sptr, endp);
  248.     if ( val1 <= 0xf ) goto d2;
  249. d1:    /* Fast check for common case */
  250.     if ( sptr >= endp ) goto x1;    /* no last char to save */
  251.     save_last = *endp;
  252.     *endp = ' ';            /* force exit from fast loop */
  253. f1:    if ( (val1 = decoder[sptr[1]]) <= 0xf &&
  254.          (val2 = decoder[sptr[2]]) <= 0xf
  255.        )
  256.        {    sptr += 2;
  257.         *ptr++ = (val1 << 4) + val2;
  258.         if ( ptr < limit ) goto f1;
  259.         *endp = save_last;
  260.         goto px;
  261.        }
  262.     *endp = save_last;
  263. x1:    while ( (val1 = decoder[ch = sgetc_inline(s, sptr, endp)]) > 0xf )
  264.        {    if ( val1 == ctype_eof )
  265.            {    code = ch; *odd_digit = -1; goto ended;    }
  266.         else if ( val1 != ctype_space && !ignore_garbage )
  267.            {    sptr--; *odd_digit = -1; goto err;    }
  268.        }
  269. d2:    while ( (val2 = decoder[ch = sgetc_inline(s, sptr, endp)]) > 0xf )
  270.        {    if ( val2 == ctype_eof )
  271.            {    code = ch; *odd_digit = val1; goto ended;    }
  272.         else if ( val2 != ctype_space && !ignore_garbage )
  273.            {    sptr--; *odd_digit = val1; goto err;    }
  274.        }
  275.     *ptr++ = (val1 << 4) + val2;
  276.     if ( ptr < limit ) goto d1;
  277. px:    *nread = rlen;
  278.     s_end_inline(s, sptr, endp);
  279.     return 0;
  280. err:    code = ERRC;
  281. ended:    *nread = ptr - str;
  282.     s_end_inline(s, sptr, endp);
  283.     return code;
  284. }
  285.  
  286. /* Skip a specified distance in a stream. */
  287. /* Return 0, EOFC, or ERRC. */
  288. int
  289. spskip(register stream *s, long n)
  290. {    if ( n < 0 || !s_is_reading(s) ) return ERRC;
  291.     if ( s_can_seek(s) )
  292.         return sseek(s, stell(s) + n);
  293.     while ( sbufavailable(s) < n )
  294.     {    int code;
  295.         n -= sbufavailable(s) + 1;
  296.         s->cptr = s->endptr;
  297.         if ( s->end_status )
  298.             return s->end_status;
  299.         code = sgetc(s);
  300.         if ( code < 0 ) return code;
  301.     }
  302.     s->cptr += n;
  303.     return 0;
  304. }    
  305.  
  306. /* ------ String streams ------ */
  307.  
  308. /* Initialize a stream for reading a string. */
  309. void
  310. sread_string(register stream *s, const byte *ptr, uint len)
  311. {    static const stream_procs p =
  312.        {    s_string_available, s_string_seek,
  313.         s_std_read_flush, s_std_null,
  314.         s_string_read_buf, NULL
  315.        };
  316.     s_string_init(s, (byte *)ptr, len, &p);
  317.     s->modes = s_mode_read + s_mode_seek;
  318. }
  319. /* Handle end-of-buffer when reading from a string. */
  320. private int
  321. s_string_read_buf(stream *s)
  322. {    s->cptr = s->endptr;
  323.     s->end_status = EOFC;
  324.     return EOFC;
  325. }
  326. /* Return the number of available bytes when reading from a string. */
  327. private int
  328. s_string_available(stream *s, long *pl)
  329. {    *pl = sbufavailable(s);
  330.     if ( *pl == 0 ) *pl = -1;    /* EOF */
  331.     return 0;
  332. }
  333.  
  334. /* Initialize a stream for writing a string. */
  335. void
  336. swrite_string(register stream *s, byte *ptr, uint len)
  337. {    static const stream_procs p =
  338.        {    s_std_noavailable, s_string_seek,
  339.         s_std_write_flush, s_std_null,
  340.         NULL, s_string_write_buf
  341.        };
  342.     s_string_init(s, ptr, len, &p);
  343.     s->modes = s_mode_write + s_mode_seek;
  344. }
  345. /* Handle end-of-buffer when writing a string. */
  346. private int
  347. s_string_write_buf(stream *s)
  348. {    s->cptr = s->endptr;
  349.     s->end_status = EOFC;
  350.     return EOFC;
  351. }
  352.  
  353. /* Seek in a string.  Return 0 if OK, ERRC if not. */
  354. private int
  355. s_string_seek(register stream *s, long pos)
  356. {    if ( pos < 0 || pos > s->bsize ) return ERRC;
  357.     s->cptr = s->cbuf + pos - 1;
  358.     return 0;
  359. }
  360.  
  361. /* Private initialization */
  362. private void
  363. s_string_init(register stream *s, byte *ptr, uint len, const stream_procs *pp)
  364. {    s_std_init(s, ptr, len, pp, s_mode_write);
  365.     s->end_status = EOFC;        /* this is all there is */
  366.     s->file = 0;            /* not a file stream */
  367. }
  368.  
  369. /* ------ File streams ------ */
  370.  
  371. /* Initialize a stream for reading an OS file. */
  372. void
  373. sread_file(register stream *s, FILE *file, byte *buf, uint len)
  374. {    static const stream_procs p =
  375.        {    s_file_available, s_file_read_seek,
  376.         s_std_read_flush, s_file_read_close,
  377.         s_file_read_buf, NULL
  378.        };
  379.     s_std_init(s, buf, len, &p,
  380.            (file == stdin ? s_mode_read : s_mode_read + s_mode_seek));
  381.     s->file = file;
  382. }
  383. /* Procedures for reading from a file */
  384. private int
  385. s_file_read_buf(register stream *s)
  386. {    int nread;
  387.     if ( s_can_seek(s) )
  388.         s->position = ftell(s->file);
  389.     nread = fread(s->cbuf, 1, s->bsize, s->file);
  390.     s->cptr = s->cbuf - 1;
  391.     s->end_status = (ferror(s->file) ? ERRC : feof(s->file) ? EOFC : 0);
  392.     if ( nread <= 0 ) nread = 0;
  393.     s->endptr = s->cptr + nread;
  394.     return 0;
  395. }
  396. private int
  397. s_file_available(register stream *s, long *pl)
  398. {    *pl = sbufavailable(s);
  399.     if ( sseekable(s) )
  400.        {    long pos, end;
  401.         pos = ftell(s->file);
  402.         if ( fseek(s->file, 0L, 2) ) return ERRC;
  403.         end = ftell(s->file);
  404.         if ( fseek(s->file, pos, 0) ) return ERRC;
  405.         *pl += end - pos;
  406.         if ( *pl == 0 ) *pl = -1;    /* EOF */
  407.        }
  408.     else
  409.        {    if ( *pl == 0 && feof(s->file) ) *pl = -1;    /* EOF */
  410.        }
  411.     return 0;
  412. }
  413. private int
  414. s_file_read_seek(register stream *s, long pos)
  415. {    uint end = s->endptr - s->cbuf + 1;
  416.     long offset = pos - s->position;
  417.     if ( offset >= 0 && offset <= end )
  418.        {    /* Staying within the same buffer */
  419.         s->cptr = s->cbuf + offset - 1;
  420.         return 0;
  421.        }
  422.     if ( fseek(s->file, pos, 0) != 0 )
  423.         return ERRC;
  424.     s->endptr = s->cptr = s->cbuf - 1;
  425.     s->end_status = 0;
  426.     return 0;
  427. }
  428. private int
  429. s_file_read_close(stream *s)
  430. {    return fclose(s->file);
  431. }
  432.  
  433. /* Initialize a stream for writing an OS file. */
  434. void
  435. swrite_file(register stream *s, FILE *file, byte *buf, uint len)
  436. {    static const stream_procs p =
  437.        {    s_std_noavailable, s_file_write_seek,
  438.         s_file_write_flush, s_file_write_close,
  439.         NULL, s_file_write_buf
  440.        };
  441.     s_std_init(s, buf, len, &p,
  442.            (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  443.     s->file = file;
  444.    }
  445. /* Procedures for writing on a file */
  446. private int
  447. s_file_write_buf(register stream *s)
  448. {    uint count = s->cptr + 1 - s->cbuf;
  449.     uint write_count = fwrite(s->cbuf, 1, count, s->file);
  450.     if ( s_can_seek(s) )
  451.         s->position = ftell(s->file);
  452.     s->cptr = s->cbuf - 1;
  453.     if ( write_count != count )
  454.        {    s->end_status = (ferror(s->file) ? ERRC : EOFC);
  455.         s->endptr = s->cptr;
  456.        }
  457.     else
  458.         s->endptr = s->cptr + s->bsize;
  459.     return 0;
  460. }
  461. private int
  462. s_file_write_seek(stream *s, long pos)
  463. {    /* Output files are not positionable */
  464.     return ERRC;
  465. }
  466. private int
  467. s_file_write_flush(register stream *s)
  468. {    int result = s_file_write_buf(s);
  469.     fflush(s->file);
  470.     return result;
  471. }
  472. private int
  473. s_file_write_close(register stream *s)
  474. {    s_file_write_buf(s);
  475.     return fclose(s->file);
  476. }
  477.